package com.skyd.andengine;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.IUpdateHandler.IUpdateHandlerMatcher;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.IEntity.IEntityCallable;
import org.anddev.andengine.entity.IEntity.IEntityMatcher;
import org.anddev.andengine.entity.modifier.IEntityModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.Scene.ITouchArea.ITouchAreaMatcher;
import org.anddev.andengine.entity.scene.background.IBackground;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.Transformation;

import com.skyd.core.draw.DrawHelper;

import android.graphics.Path;
import android.graphics.Region;

/**
 * 游戏场景
 * 
 * @author SkyD
 * 
 */
public class GameScene extends Scene {

	/**
	 * 添加屏幕区域触摸监听器
	 * 
	 * @param xPos
	 *            区域路径X坐标数组
	 * @param yPos
	 *            区域路径y坐标数组
	 * @param listener
	 *            屏幕区域触摸监听器
	 */
	public void addOnSceneAreaTouchListener(int[] xPos, int[] yPos,
			IOnSceneAreaTouchListener listener) {
		Path p = new Path();
		for (int i = 0; i < Math.min(xPos.length, yPos.length); i++) {
			if (i == 0)
				p.moveTo(xPos[i], yPos[i]);
			else
				p.lineTo(xPos[i], yPos[i]);
		}
		p.close();
		addOnSceneAreaTouchListener(p, listener);
	}

	/**
	 * 添加屏幕区域触摸监听器
	 * 
	 * @param p
	 *            区域路径
	 * @param listener
	 *            屏幕区域触摸监听器
	 */
	public void addOnSceneAreaTouchListener(Path p,
			IOnSceneAreaTouchListener listener) {
		sceneAreaTouchTable.put(listener, DrawHelper.getRegionFromPath(p));
	}

	/**
	 * 移除屏幕区域触摸监听器
	 * 
	 * @param listener
	 *            屏幕区域触摸监听器
	 */
	public void removeOnSceneAreaTouchListener(
			IOnSceneAreaTouchListener listener) {
		if (sceneAreaTouchTable.containsKey(listener))
			sceneAreaTouchTable.remove(listener);
	}

	Hashtable<IOnSceneAreaTouchListener, Region> sceneAreaTouchTable = new Hashtable<IOnSceneAreaTouchListener, Region>();

	@Override
	public boolean onSceneTouchEvent(TouchEvent pSceneTouchEvent) {
		boolean b = true;
		Enumeration<IOnSceneAreaTouchListener> e = sceneAreaTouchTable.keys();
		while (e.hasMoreElements()) {
			IOnSceneAreaTouchListener l = e.nextElement();
			if (sceneAreaTouchTable.get(l).contains(
					(int) pSceneTouchEvent.getX(),
					(int) pSceneTouchEvent.getY())) {
				b = l.onSceneAreaTouchEvent(this, pSceneTouchEvent) && b;
			}
		}
		return super.onSceneTouchEvent(pSceneTouchEvent) && b;
	}

	/**
	 * 将纹理区域设为背景
	 * 
	 * @param texture
	 *            纹理区域
	 */
	public void setBackground(TextureRegion texture) {
		Sprite bs = new Sprite(0, 0, texture);
		SpriteBackground bg = new SpriteBackground(bs);
		setBackground(bg);
	}
	
}
